<?php
namespace app\mamx_forum_admin\controller;
use think\facade\Db;
use think\facade\Filesystem;
use think\Controller;

class Bindeal extends \app\BaseController{
	// 检查 mid 合法性
	public function checkMID($mid){
		$re = Db::name('mes')
		    ->where('m_id',$mid)
		    ->find();
		if($re == null){
		     // 不合法
		    $this->error('你访问的页面不存在');
		}
	}
	// 检查 sid 合法性
	public function checkSID($sid){
	    $re = Db::name('section')
	        ->field('s_name')
	        ->where('s_id',$sid)
	        ->find();
	    if($re == null){
	        // 不合法
	        $this->error('你访问的页面不存在','index/index');
	    }else{
	        return($re['s_name']);
	    }
	}
	// 渲染帖子回收站页面
	public function binMesDeal($sid=0){
		$this->checkadmin();
		// 查询版块
		$sec = $this->showSec();
		if (input('search') ==''){
			if($sid == 0){
			$thisSec = '全部';
			$mes = Db::name('mes')
				->where('mbin',0)
				->select();
		}else{
			// 检查帖子合法性
			$thisSec=$this->checkSId($sid);
			$mes = Db::name('mes')
				->where('mbin',0)
				->where('m_sid',$sid)
				->select();
		}
		}else{
			if($sid==0){
				// 有搜索没有sid的情况
				$thisSec = '全部';
				$search=input('search');
				// $sid=2;
				$mes = Db::name('mes')
					->where('m_title','like','%'.$search.'%')
					->select();
			}else{
				// 检查帖子合法性
				$thisSec=$this->checkSId($sid);
				// 有搜索有sid的情况下
				$search= input('search');
				$mes = Db::name('mes')
					->where('m_title','like','%'.$search.'%')
					->where('m_sid',input('sid'))
					->select();
			}
		}
		
		return view('',['sec'=>$sec,'mes'=>$mes,'thisSec'=>$thisSec]);
	}
	// 执行帖子回收
	public function binMesDel(){
		// 检查登录状态
		$this->checkadmin();
		$res = Db::name('res')
			->where('r_mid',input('mid'))
			->select();
		//查询回复条数
		$resCount = count($res);
		// dump($resCount);
		if($resCount == 0){
			// 无回复
		}else{
			$mesmodi = Db::view('mes','m_id,mbin,mreason')
					->view('res','rbin,rreason','res.r_mid=mes.m_id')
					->where('m_id',input('mid'))
					->where('r_mid',input('mid'))
					->update(['res.rbin'=>0,'mes.mbin'=>0,'mes.mreason'=>'删除帖子','res.rreason'=>'帖子被放入回收站','mes.mdelat'=>date('Y-m-d H:i'),'res.rdelat'=>date('Y-m-d H:i')]);
			// dump($mesmodi);
		}
		$mesmodi = Db::name('mes')
				->where('m_id',input('mid'))
				->update(['mbin'=>0,'mreason'=>'删除帖子','mdelat'=>date('Y-m-d H:i')]);
		// dump($mesmodi);
		if($mesmodi==1){
			$this->success('帖子放入回收站成功','deal/mesdeal');
		}else{
			$this->success('帖子和回复放入回收站成功','deal/mesdeal');
		}
	}
	// 执行还原帖子
	public function binMesBack(){
		// 检查登录状态
		$this->checkadmin();
		$res = Db::name('res')
			->where('r_mid',input('mid'))
			->select();
		//查询回复条数
		$resCount = count($res);
		// dump($resCount);
		if($resCount == 0){
			// 无回复
		}else{
			$mesmodi = Db::view('mes','m_id,mbin,mreason')
					->view('res','rbin,rreason','res.r_mid=mes.m_id')
					->where('m_id',input('mid'))
					->where('r_mid',input('mid'))
					->update(['res.rbin'=>1,'mes.mbin'=>1,'mes.mreason'=>'还原帖子','res.rreason'=>'帖子被还原','mes.mdelat'=>date('Y-m-d H:i'),'res.rdelat'=>date('Y-m-d H:i')]);
			// dump($mesmodi);
		}
		if($mesmodi==1){
			$this->success('帖子还原成功','bindeal/binmesdeal');
		}else{
			$this->success('帖子和回复还原成功','bindeal/binmesdeal');
		}
	}
	// 渲染版块回收站页面
	public function binSecDeal($sid=0){
		// 检查登录状态
		$this->checkadmin();
		// 查询版块
		$sec=$this->showSec();
		// 搜索
		$search = input('search');
		if(input('search') == ''){
			// 没有搜索
			$sec = Db::name('section')
				->select();
			return view('',['sec'=>$sec,'search'=>$search]);
		}else{
			$sec =Db::name('section')
				->where('s_name','like','%'.$search.'%')
				->select();
			return view('',['sec'=>$sec,'search'=>$search]);
		}
	}
	// I组 执行板块回收
	public function  binSecDel(){
		$this->checkadmin();
		$sec = $this->showSec();
		$section = Db::name("section")  
		         ->select();
		$mes = Db::name('mes')
			->where('m_sid',input('sid'))
			->select();
		$mescount =count($mes);
		// echo $mescount;
		if($mescount!=0){
			// 无帖子
			$modisec = Db::name("section")
				->where('s_id',input('sid'))
				->update(['sbin'=>'0','sdelat'=>date('Y-m-d H:i'),'sreason'=>'删除版块']);
		// echo $modisec;
		}else{
			// 有帖子
			$res = Db::view('res','r_mid')
				->view('mes','m_id = r_mid')
				->view('section','s_id = mes_sid')
				->where('s_id',input('sid'))
				->select();
			// echo $res;
			$rescount =count($res);
			// echo $rescount;
			if($rescount==0){
				// 帖子无回复
				$modisec = Db::view('section','s_id,sbin,sdelat')
							->view('mes','mbin,mdelat,m_id,mreason','mes.m_sid=section.s_id')
							// ->view('res','rbin,rdelat,rreason','res.r_mid=mes.m_id')
							->where('m_sid',input('sid'))
							->update(['sbin'=>0,'sdelat'=>date('Y-m-d H:i'),'mbin'=>'0','mdelat'=>date('Y-m-d H:i'),'mreason'=>'由于板块被放入回收站帖子也被放入回收站']);													
			}else{
				// 有回复
				$modisec = Db::view('section','s_id,sbin,sdelat')
							->view('mes','mbin,mdelat,m_id,mreason','mes.m_sid=section.s_id')
							->view('res','rbin,rdelat,rreason','res.r_mid=mes.m_id')
							->where('m_sid',input('sid'))
							->update(['sbin'=>0,'sdelat'=>date('Y-m-d H:i'),'mbin'=>'0','mdelat'=>date('Y-m-d H:i'),'mreason'=>'由于板块被放入回收站帖子也被放入回收站','rbin'=>0,'rdelat'=>date('Y-m-d H:i'),'rreason'=>'由于板块被放入回收站回复也被放入回收站']);
			}
		}
		// echo $modisec;
		if($modisec==1){
			$this->success("板块里的帖子以及帖子里的回复放入回收站成功！");
		}else{
			$this->success("板块里的帖子以及帖子里的回复放入回收站成功！");
		}
	}
	// I组 版块还原
	public function binSecBack(){
		$this->checkadmin();
		$sec = $this->showSec();
		// $section = Db::name("section")  
		//          ->select();
		$mes = Db::name('mes')
			->where('m_sid',input('sid'))
			->select();
		// $mid = $mes['m_id'];
		$mescount =count($mes);
		echo $mescount;
		if($mescount!=0){
			// 无帖子
		}else{
		 	$res = Db::view('res','r_mid,rbin')
		 		->view('mes','mes.m_id=res.r_mid')
		 		// ->where('res.r_mid'=='mes.m_id')
				->select();
			$rescount = count($res);
		echo $rescount;
		dump($res);
			if($rescount==0){
				// 帖子无回复			 				 		
			}else{
				// 帖子有回复
				$modisec = Db::name('res')
						->where('r_mid',$mid)
						->update(['res.rbin'=>'1']);
			}
		$modisec =Db::name('mes')
		 		->where('m_sid',input('sid'))
		 		->update(['mes.mbin'=>'1']);
		}
		$modisec = Db::name("section")
		 		->where('s_id',input('sid'))
		 		->update(['sbin'=>'1']);
		// echo $modisec;
		if($modisec==1){
		 	$this->success("还原版块成功！");
		 }else{
		 	$this->success("还原版块及其相关帖子和回复成功！");
		 }
	}
	// 渲染回复回收站页面
	public function binResDeal($sid=0){
		$this->checkadmin();
		// 查询版块
		$sec = $this->showSec();
		if (input('search') ==''){
			if($sid == 0){
			$thisSec = '全部';
			$rMes = Db::view('mes','m_id,m_sid,m_title')
				->view('res','r_id,r_content,r_createat,r_unick,rdelat,rreason','r_mid=mes.m_id')
				->where('res.rbin',0)
				->select();		
		}else{
			// 检查帖子合法性
			$thisSec=$this->checkSId($sid);
			$rMes = Db::view('mes','m_id,m_sid,m_title')
				->view('res','r_id,r_content,r_createat,r_unick,rdelat,rreason','r_mid=mes.m_id')
				->where('res.rbin',0)
				->where('mes.m_sid',input('sid'))
				->select();
		}}else{
			if($sid==0){
				// 有搜索没有sid的情况
				$thisSec = '全部';
				$search=input('search');
				// $sid=2;
				$rMes = Db::view('mes','m_id,m_sid,m_title')
					->view('res','r_id,r_content,r_createat,r_unick,rdelat,rreason','r_mid=mes.m_id')
					->where('mes.m_title','like','%'.$search.'%')
					->select();
			}else{
				// 检查帖子合法性
				$thisSec=$this->checkSId($sid);
				// 有搜索有sid的情况下
				$search= input('search');
				$rMes = Db::view('mes','m_id,m_sid,m_title')
					->view('res','r_id,r_content,r_createat,r_unick,rdelat,rreason','r_mid=mes.m_id')
					->where('res.rbin',0)
					->where('mes.m_sid',input('sid'))
					->where('mes.m_title','like','%'.$search.'%')
					->select();
			}
		}
		return view('',['sec'=>$sec,'rmes'=>$rMes,'thisSec'=>$thisSec]);
	}
	// 执行回复回收
	public function binResDel(){
		$sec = $this->showSec();
		// 查询回复
		$resmodi = Db::name('res')
			->where('r_id',input('rid'))
			->update(['rbin'=>0,'rreason'=>'回复放入回收站','rdelat'=>date('Y-m-d H:i')]);
		dump($resmodi);
		if(!$resmodi){
			$this->error('回复放入回收站失败');
		}else{
			$this->success('回复放入回收站成功');
		}
	}
	// 执行回复还原
	public function binResBack(){
		$sec = $this->showSec();
		// 查询回复
		$resmodi = Db::name('res')
			->where('r_id',input('rid'))
			->update(['rbin'=>1,'rreason'=>'还原回复','rdelat'=>date('Y-m-d H:i')]);
		dump($resmodi);
		if(!$resmodi){
			$this->error('还原回复失败');
		}else{
			$this->success('还原回复成功','deal/mesdeal');
		}
	}
}